package org.lora.mvc.security.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.lora.core.context.WebContext;
import org.lora.exception.WebSecurityException;
import org.lora.mvc.security.ISecurityService;

/**
 * <p>
 * 类名:SecurityFilter
 * </p>
 * 描述:安全认证过滤器<br>
 * 创建时间:2016年7月29日 下午4:39:58<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年7月29日 Bladnir 创建<br>
 */
@WebFilter("/*")
public class SecurityFilter implements Filter {

	@Override
	public void destroy() {
		// just do nothing

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		WebContext webContext = WebContext.getInstance();
		ISecurityService securityService = webContext.getSecurityContext().getService();
		try {
			securityService.doFilter(req, res, chain);
		} catch (WebSecurityException e) {
			throw new ServletException(e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// just do nothing
	}

}
